package com.seecoder.BlueWhale.controller;

import com.seecoder.BlueWhale.service.UserService;
import com.seecoder.BlueWhale.util.SecurityUtil;
import com.seecoder.BlueWhale.vo.ResultVO;
import com.seecoder.BlueWhale.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    SecurityUtil securityUtil;

    @PostMapping("/register")
    public ResultVO<Boolean> register(@RequestBody UserVO userVO){
        log.warn("User[{}] begins to register", userVO.getPhone());
        return ResultVO.buildSuccess(userService.register(userVO));
    }

    @PostMapping("/login")
    public ResultVO<String> login(@RequestParam("phone") String phone, @RequestParam("password") String password){
        log.warn("User[{}] begins to login", phone);
        return ResultVO.buildSuccess(userService.login(phone, password));
    }

    @GetMapping
    public ResultVO<UserVO> getInformation(){
        log.info("Begin to get user info.");
        ResultVO<UserVO> ans = ResultVO.buildSuccess(userService.getInformation());
        log.info("End to get user info.");
        return ans;
    }

    @PostMapping
    public ResultVO<Boolean> updateInformation(@RequestBody UserVO userVO){
        log.warn("User[{}] begins to update information", securityUtil.getUserId());
        ResultVO<Boolean> ans = ResultVO.buildSuccess(userService.updateInformation(userVO));
        log.warn("User[{}] ends updated information", securityUtil.getUserId());
        return ans;
    }

    @PostMapping("/info")
    public ResultVO<UserVO> getInformation(@RequestParam("id") int id) {
        log.info("Begin to get user info.");
        ResultVO<UserVO> ans = ResultVO.buildSuccess(userService.getInformation(id));
        log.info("End to get user info.");
        return ans;
    }

}
